﻿using System;

namespace Elca.MvvmHelpers {

    /// <summary>
    /// Base class for View Models
    /// </summary>
    // Many thanks to Mark Smith for his Mvvm helpers, see, http://www.julmar.com/blog/mark/2009/04/17/WPFMVVMHelperLibraryWPFMVVMTestability.aspx
    // many elements from this class come from there
    public class BaseViewModel : BaseNotifyableObject {

        /// <summary>
        /// Are we running in design mode ? this is valid iff the view model is set through <c>ViewModelCreatorExtension</c>
        /// </summary>
        public static bool IsInDesignMode { get; set; }

        /// <summary>
        /// This event should be raised to close the view.  Any view tied to this
        /// ViewModel should register a handler on this event and close itself when
        /// this event is raised.  If the view is not bound to the lifetime of the
        /// ViewModel then this event can be ignored.
        /// </summary>
        // Taken from the Mvvm helpers of Mark Smith, http://www.julmar.com/blog/mark/2009/04/17/WPFMVVMHelperLibraryWPFMVVMTestability.aspx
        public event EventHandler<CloseRequestEventArgs> CloseRequest = delegate { };

        /// <summary>
        /// This event should be raised to activate the UI.  Any view tied to this
        /// ViewModel should register a handler on this event and close itself when
        /// this event is raised.  If the view is not bound to the lifetime of the
        /// ViewModel then this event can be ignored.
        /// </summary>
        public event EventHandler ActivateRequest = delegate { };

        /// <summary>
        /// This raises the CloseRequest event to close the UI.
        /// </summary>
        public virtual void RaiseCloseRequest() {
            CloseRequest(this, new CloseRequestEventArgs(null));
        }

        /// <summary>
        /// This raises the CloseRequest event to conditionnally close the UI.
        /// </summary>
        public virtual void RaiseCloseRequest(bool? dialogResult) {
            CloseRequest(this, new CloseRequestEventArgs(dialogResult));
        }

        /// <summary>
        /// This raises the ActivateRequest event to activate the UI.
        /// </summary>
        public virtual void RaiseActivateRequest() {
            ActivateRequest(this, EventArgs.Empty);
        }
    }

    /// <summary>
    /// This is used to send result parameters to a CloseRequest
    /// </summary>
    // Taken from the Mvvm helpers of Mark Smith, http://www.julmar.com/blog/mark/2009/04/17/WPFMVVMHelperLibraryWPFMVVMTestability.aspx
    public class CloseRequestEventArgs : EventArgs {
        ///<summary>
        /// Final result for ShowDialog
        ///</summary>
        public bool? Result { get; private set; }
        internal CloseRequestEventArgs(bool? result) {
            Result = result;
        }
    }

}
